#!/usr/bin/env bash

# replace_file.sh

# This file is part of Leah (Language editor and helper).

# Leah is part of Faridayix, an artificially intelligent computer
# operating system.

# Copyright (C) 2010 Scott C. MacCallum
# scottmaccal@gmail.com

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

function replace_file
{
  clear

  printf "Would you like to replace $file_name with this new file? "

  echo -e -n $bold_font

  read replace_file

  echo -e -n $default_colors

    if [[ "$replace_file" == "yes" || "$replace_file" == "y" ]]; then
      printf "\n"

      printf "I will now attempt to replace $file_name with this new file..."

      touch $file_name 2> /dev/null

        if [ "$?" = "0" ]; then
          printf "\n"
                
          printf "\n"

          printf "Replacement of $file_name was successful."

          edit_new_file
            
        elif [ "$?" = "1" ]; then
          printf "\n"

          printf "\n"

          echo -e -n $reverse_colors

          echo -e -n $bold_font

          printf "$file_name could not be replaced."

          echo -e -n $default_colors

          printf "\n"

          printf "\n"

          printf "Perhaps you do not have permission."

          exit 1

        else
          printf "\n"

          echo -e -n $reverse_colors

          echo -e -n $bold_font

          printf "I do not understand what you said."

          echo -e -n $default_colors

          printf "\n"

          printf "\n"

          echo -e -n "Please answer me with a ${reverse_colors}${bold_font}y${default_colors}es or ${reverse_colors}${bold_font}n$\
{default_colors}o."

          echo -e -n $default_colors

          replace_file
        fi

          if [[ "$replace_file" == "no" || "$replace_file" == "n" ]]; then
            rename_new_file

          else
            printf "\n"

            echo -e -n $reverse_colors

            echo -e -n $bold_font

            printf "I do not understand what you said."

            echo -e -n $default_colors

            printf "\n"

            printf "\n"

            echo -e -n "Please answer me with a ${reverse_colors}${bold_font}y${default_colors}es or ${reverse_colors}${bold_font}n${default_colors}o."

            echo -e -n $default_colors

            replace_file
          fi
    fi
}
#EOF
